﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1007
{
    [Plug(ID = "cMES1007",
        Name = "ESOP信息",
        Description = "ESOP资料维护",
        Parent = "0,基础资料")]
    public partial class cMES1007 : StdView
    {
        public cMES1007()
        {
            InitializeComponent();
        }

        private void InitailGroupNameList()
        {
            ExecutionResult<dynamic> result;
             result = Services.ServiceMethod.GetGroupNameList();
            if(result.Status && result.Data !=null)
            {
                this.cmbGroupName.Items.Clear();
                foreach(var tmpGroup in result.Data)
                {
                    this.cmbGroupName.Items.Add(tmpGroup.group_name.ToString());
                }
            }            
        }

        private void GetSOPInfoList()
        {
            var _group = this.cmbGroupName.Text.Trim().ToUpper();
            var _skuName = this.txtSKUName.Text.Trim().ToUpper();
            ExecutionResult<dynamic> _res = new ExecutionResult<dynamic>();

            _res = Services.ServiceMethod.GetSOPInfoList(_group, _skuName);
            if (!_res.Status)
            {
                datalist.Rows.Clear();
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            datalist.DataSource = _res.Data;
            datalist.ClearSelection();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetSOPInfoList();
        }

        private void cMES1007_Load(object sender, EventArgs e)
        {
            //initial group name
            this.InitailGroupNameList();
            //get SOP info
            this.GetSOPInfoList();
        }

        private void datalist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                frmESOPEdit _esopEdit = new frmESOPEdit("EDIT", datalist.SelectedRows[0]);
                DialogResult dialog = _esopEdit.ShowDialog();
                if (dialog == DialogResult.OK)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                else if (dialog == DialogResult.No)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                // 刷新数据
                this.GetSOPInfoList();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.ESOPInfoChange(sender);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.ESOPInfoChange(sender);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (datalist.SelectedRows.Count < 1)
            {
                this.ShowMessage("请选择一笔需要删除的资料..", MsgType.NG);
                return;
            }
            var _id = datalist.SelectedRows[0].Cells[0].Value.ToString();

            DialogResult dialog = MessageBox.Show(
                "确定要删除吗?", "系统提示",
                MessageBoxButtons.YesNo,
                 MessageBoxIcon.Warning,
                  MessageBoxDefaultButton.Button2);
            if (dialog == DialogResult.Yes)
            {
                var result = Services.ServiceMethod.DeleteESOPInfo(_id);
                if (result.Status)
                {
                    this.ShowMessage("删除数据成功..", MsgType.OK);
                    //刷新数据
                    this.GetSOPInfoList();
                }
            }
        }

        private void ESOPInfoChange(object sender)
        {
            frmESOPEdit _esopEdit = default(frmESOPEdit);
            var clickBtnTag = ((ToolStripButton)sender).Tag;
            if (clickBtnTag.Equals("ADD"))
            {
                _esopEdit = new frmESOPEdit(clickBtnTag.ToString());
                DialogResult result = _esopEdit.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据新增成功..", MsgType.OK);
                    //refresh data
                    this.GetSOPInfoList();
                }
                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据新增失败..", MsgType.NG);
                    //refresh data
                    this.GetSOPInfoList();
                }
            }
            else if (clickBtnTag.Equals("EDIT"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    this.ShowMessage("请选择一笔需要修改的资料..", MsgType.NG);
                    return;
                }
                _esopEdit = new frmESOPEdit(clickBtnTag.ToString(), datalist.SelectedRows[0]);
                DialogResult result = _esopEdit.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功..", MsgType.OK);
                    //refresh data
                    this.GetSOPInfoList();
                }
                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败..", MsgType.NG);
                    //refresh data
                    this.GetSOPInfoList();
                }
            }
        }
    }
}
